package com.peam.system.service.impl;

import com.peam.common.constant.UserConstants;
import com.peam.common.core.text.Convert;
import com.peam.common.utils.StringUtils;
import com.peam.system.domain.Engineering;
import com.peam.system.mapper.EngineeringMapper;
import com.peam.system.service.IEngineeringService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工程信息 服务层处理
 * 
 * @author peam
 */
@Service
public class EngineeringServiceImpl implements IEngineeringService
{
    @Resource
    private EngineeringMapper engineeringMapper;

    @Resource
    private RuntimeService runtimeService;
    @Resource
    HistoryService historyService;

    /**
     * 查询工程
     *
     * @param id 主键
     * @return 工程
     */
    public Engineering selectEngineeringById(Long id)
    {
        return engineeringMapper.selectEngineeringById(id);
    }
    /**
     * 查询所有工程
     * 
     * @return 工程列表
     */
    @Override
    public List<Engineering> selectEngineeringList(Engineering engineering)
    {
        return engineeringMapper.selectEngineeringList(engineering);
    }
//    @Override
//    public List<Engineering> selectEngineeringAll()
//    {
//        return engineeringMapper.selectEngineeringAll();
//    }
    /**
     * 删除工程信息
     * 
     * @param id 工程ID
     * @return 结果
     */
    @Override
    public int deleteEngineeringById(Long id)
    {
        return engineeringMapper.deleteEngineeringById(id);
    }

    /**
     * 批量删除岗位信息
     *
     * @param ids
     * @return 结果
     */
    @Override
    public int deleteEngineeringByIds(String ids)
    {
        String[] keys = Convert.toStrArray(ids);
        for (String key : keys) {
            ProcessInstance process = runtimeService.createProcessInstanceQuery().processDefinitionKey("engineering").processInstanceBusinessKey(key).singleResult();
            if (process != null) {
                runtimeService.deleteProcessInstance(process.getId(), "删除");
            }
            // 删除历史数据
            HistoricProcessInstance history = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("engineering").processInstanceBusinessKey(key).singleResult();
            if (history != null){
                historyService.deleteHistoricProcessInstance(history.getId());
            }
            engineeringMapper.deleteEngineeringById(Long.parseLong(key));
        }
        return keys.length;
    }

    /**
     * 新增工程
     * 
     * @param engineering 工程
     * @return 结果
     */
    @Override
    public int insertEngineering(Engineering engineering)
    {
        return engineeringMapper.insertEngineering(engineering);
    }

    /**
     * 修改工程
     * 
     * @param engineering 工程
     * @return 结果
     */
    @Override
    public int updateEngineering(Engineering engineering)
    {
        return engineeringMapper.updateEngineering(engineering);
    }

//    @Override
//    public boolean checkEngineeringNameUnique(Engineering engineering) {
//        return false;
//    }



//    /**
//     * 校验工程名称是否唯一
//     *
//     * @param engineering 工程信息
//     * @return 结果
//     */
//    @Override
//    public boolean checkEngineeringNameUnique(Engineering engineering)
//    {
//        Long engineeringId = StringUtils.isNull(engineering.getEngineeringId()) ? -1L : engineering.getEngineeringId();
//        Engineering info = engineeringMapper.checkEngineeringNameUnique(engineering.getEngineeringName());
//        if (StringUtils.isNotNull(info) && info.getEngineeringId().longValue() != engineeringId.longValue())
//        {
//            return UserConstants.NOT_UNIQUE;
//        }
//        return UserConstants.UNIQUE;
//    }
//
//    /**
//     * 校验工程编码是否唯一
//     *
//     * @param engineering 工程信息
//     * @return 结果
//     */
//    @Override
//    public boolean checkEngineeringCodeUnique(Engineering engineering)
//    {
//        Long engineeringId = StringUtils.isNull(engineering.getEngineeringId()) ? -1L : engineering.getEngineeringId();
//        Engineering info = engineeringMapper.checkEngineeringCodeUnique(engineering.getEngineeringCode());
//        if (StringUtils.isNotNull(info) && info.getEngineeringId().longValue() != engineeringId.longValue())
//        {
//            return UserConstants.NOT_UNIQUE;
//        }
//        return UserConstants.UNIQUE;
//    }
}
